package com.jake.rabbit.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Queue;

@Aspect
@Slf4j
@Component
public class ConcurrentQueueAddAspect {

    @Autowired
    @Qualifier("rabbitConcurrentQueue")
    private Queue<LocalDateTime> rabbitConcurrentQueue;

    @Pointcut("execution(public java.util.Map<String, Object> com.jake.rabbit.service.RabbitRfcService.getResponseMap(..))")
    public void increase(){}

    @Around("increase()")
    public Object add(ProceedingJoinPoint pjp) throws Throwable {
        boolean notFull = rabbitConcurrentQueue.offer(LocalDateTime.now());
        log.info("add: rabbitConcurrentQueue's size is {}", rabbitConcurrentQueue.size());
        if (!notFull) {
            log.error("add: rabbitConcurrentQueue is full");
            return null;
        }
        return pjp.proceed();
    }

}
